﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using LL.Business;
using NanoERP.Model;
using NanoERP.Business.BaseInfo;
using NanoERP.Common;

namespace NanoERP.Business.BillInfo
{
    public class OrderpayinfoBLL:ServiceBase
    {
        /// <summary>
        /// 获取预付款ID
        /// </summary>
        public long GetID()
        {
            var dt = DbSession.ExcuteQuery(string.Format("select MAX(ID+1) as ID from dbo.ORDERPAYINFO where ID is not null"));

            return dt != null ? Normal.ToLong(dt.Rows[0][0]) : 0;
        }
        // 根据单I订D获取预付款ID
        /// <summary>
        /// 根据单I订D获取预付款ID
        /// </summary>
        public long GetID(long _orderid)
        {
            var dt = DbSession.ExcuteQuery(string.Format("select ID from dbo.ORDERPAYINFO where Order_ID=",_orderid.ToString()));

            return dt != null ? Normal.ToLong(dt.Rows[0][0]) : 0;
        }
        /// <summary>
        /// 保存订单的预付款
        /// </summary>
        /// <param name="pay"></param>
        /// <returns></returns>
        public OrderPayinfo Edit(OrderPayinfo pay)
        {
            if (Exist(pay.ORDER_ID)==0)
            {
                this.DbSession.Insert(pay,"ID");
                pay.ID = GetID(pay.ORDER_ID);
            }
            else
            {
                this.DbSession.Update(pay);
            }
            return pay;
        }
        /// <summary>
        /// 是否存在预付款记录
        /// </summary>
        /// <param name="orderid"></param>
        /// <returns></returns>
        public int Exist(long orderid)
        {
            var dt = DbSession.ExcuteQuery(string.Format("select ID from dbo.ORDERPAYINFO where order_id="+orderid));

            return dt.Rows.Count;
        }
    }
}
